﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace HQWeb.SysManage.UniualMateral
{
    public partial class List : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            VerdictIsLogin();
            if (!IsPostBack)
            {
                string strType="";
                if (Request["type"]!=null)
                {
                    strType = Request["type"]; 
                }
                if (strType == "price")
                {
                    hidPrice.Text = Request["id"].Trim();
                }
                else
                {
                    HidMonth.Text = Request["id"].Trim();
                }
                BindData();
            }
           
        }
        //数据绑定
        private void BindData()
        {
            BLL.SysManage.T_PUB_UNIALMATIAL bll = new BLL.SysManage.T_PUB_UNIALMATIAL();
            string strMonth = HidMonth.Text;
            string strPrice = hidPrice.Text;
            GridView1.DataSource = bll.GetList(strMonth, strPrice);
            GridView1.DataBind();
        }

        #region 翻页事件
        protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            try
            {
                //strWhere = strWhere.Replace("order by operatetime DESC", "");
                int newPageIndex = 0;

                if (-2 == e.NewPageIndex)
                { // when click the "GO" Button
                    TextBox txtNewPageIndex = null;
                    GridViewRow pagerRow = GridView1.Controls[0].Controls[GridView1.Controls[0].Controls.Count - 1] as GridViewRow; // refer to PagerTemplate
                    //GridViewRow pagerRow = GridView1.BottomPagerRow; //GridView较DataGrid提供了更多的API，获取分页块可以使用BottomPagerRow 或者TopPagerRow，当然还增加了HeaderRow和FooterRow
                    if (null != pagerRow)
                    {
                        txtNewPageIndex = pagerRow.FindControl("txtNewPageIndex") as TextBox;   // refer to the TextBox with the NewPageIndex value
                    }

                    if (null != txtNewPageIndex)
                    {
                        newPageIndex = int.Parse(txtNewPageIndex.Text) - 1; // get the NewPageIndex
                    }

                    newPageIndex = newPageIndex < 0 ? 0 : newPageIndex;
                    newPageIndex = newPageIndex >= GridView1.PageCount ? GridView1.PageCount - 1 : newPageIndex;
                    GridView1.PageIndex = newPageIndex;
                }
                else if (e.NewPageIndex == -1)
                {
                    newPageIndex = 0;
                    GridView1.PageIndex = 0;
                }
                else
                {  // when click the first, last, previous and next Button
                    newPageIndex = e.NewPageIndex;
                    GridView1.PageIndex = e.NewPageIndex;
                }

                // check to prevent form the NewPageIndex out of the range


                BindData();
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        #endregion

        #region 编辑删除

        protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            string strCommand = e.CommandName;
            string strID = e.CommandArgument.ToString();
            if (strCommand.Equals("UpdateData"))  //编辑
            {
                Response.Redirect("Edit.aspx?sign=1&id=" + strID);
            }
            else if (strCommand.Equals("DelData"))  //删除
            {
                BLL.SysManage.T_PUB_UNIALMATIAL bll = new BLL.SysManage.T_PUB_UNIALMATIAL();
                bll.Delete(Convert.ToInt32(strID));
                BindData();
            }
        }

        #endregion
        //添加
        protected void btnAdd_Click(object sender, EventArgs e)
        {
            string strType = "";
            string strID = "";
            string strUrl="";
            if (Request["type"] != null)
            {
                strType = Request["type"];
            }
            if (strType == "price")
            {
                strID = Request["id"].Trim();
                strUrl = string.Format("Edit.aspx?sign=0&pricid={0}", strID);
            }
            else
            {
                strID = Request["id"].Trim();
                strUrl = string.Format("Edit.aspx?sign=0&pid={0}", strID);
            }
           
            Response.Redirect(strUrl);
        }
    }
}